/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */
package io.karim.materialtabs.sample;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.PageSliderProvider;
import ohos.agp.components.StackLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * SamplePagerAdapter
 *
 * @author hal
 * @since 2021/7/20
 */
public class SamplePagerAdapter extends PageSliderProvider {
    private final Map<Integer, Component> cacheMap = new HashMap<>();
    private final String[] TITLES = {"Item One", "Item Two", "Item Three", "Item Four", "Item Five", "Item Six", "Item Seven", "Item Eight",
            "Item Nine", "Item Ten", "Item Eleven"};
    private final ArrayList<String> mTitles;

    public SamplePagerAdapter(int numberOfTabs) {
        mTitles = new ArrayList<>();
        mTitles.addAll(Arrays.asList(TITLES).subList(0, numberOfTabs));
    }

    @Override
    public int getCount() {
        return mTitles.size();
    }

    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int position) {
        SampleFragment sampleFragment = SampleFragment.newInstance(position, componentContainer.getContext());
        componentContainer.addComponent(sampleFragment, new StackLayout.LayoutConfig(StackLayout.LayoutConfig.MATCH_PARENT, StackLayout.LayoutConfig.MATCH_PARENT));
        return sampleFragment;
    }

    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int position, Object object) {
        componentContainer.removeComponent((Component) object);
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object object) {
        return true;
    }

    @Override
    public String getPageTitle(int position) {
        return mTitles.get(position);
    }
}
